package com.xmb.quick.util;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;

public class ConditionUtil {
	
	public static <T> Wrapper<T> convert(String jsonStr, Wrapper<T> ew) throws Exception{
		if(StringUtils.isEmpty(jsonStr)) {
			return ew;
		}
		List<Condition> condition = new ArrayList<Condition>();
		try {
			condition = BeanUtil.parseObjToList(jsonStr, Condition.class);
		} catch (Exception e) {
			throw new Exception("参数异常");
		}
		for (Condition c : condition) {
			if(c.getConditionOptionVal().equals("equal")) {
				ew.eq(c.getConditionFieldVal(), c.getConditionValueVal().getValue());
			}else if(c.getConditionOptionVal().equals("like")){
				ew.like(c.getConditionFieldVal(), c.getConditionValueVal().getValue());
			}else if(c.getConditionOptionVal().equals("between")){
				ew.between(c.getConditionFieldVal(), c.getConditionValueLeftVal().getValue(), c.getConditionValueRightVal().getValue());
			}else if(c.getConditionOptionVal().equals("unequal")){
				ew.ne(c.getConditionFieldVal(), c.getConditionValueVal().getValue());
			}else if(c.getConditionOptionVal().equals("empty")){
				ew.isNull(c.getConditionFieldVal()).where(c.getConditionFieldVal() + "=''");
			}else if(c.getConditionOptionVal().equals("notempty")){
				ew.isNotNull(c.getConditionFieldVal()).where(c.getConditionFieldVal() + "!=''");
			}else if(c.getConditionOptionVal().equals("greater_than")){
				ew.gt(c.getConditionFieldVal(), c.getConditionValueVal().getValue());
			}else if(c.getConditionOptionVal().equals("less_than")){
				ew.lt(c.getConditionFieldVal(), c.getConditionValueVal().getValue());
			}else if(c.getConditionOptionVal().equals("start")){
				ew.like(c.getConditionFieldVal(), c.getConditionValueVal().getValue(), SqlLike.LEFT);
			}else if(c.getConditionOptionVal().equals("end")){
				ew.like(c.getConditionFieldVal(), c.getConditionValueVal().getValue(), SqlLike.RIGHT);
			}
		}
		return ew;
	}
	
}
